package com.daxia.ogmall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.ogmall.common.Logs;
import com.daxia.ogmall.dto.JsonResultDTO;
import com.daxia.ogmall.dto.ProductPropertiesDTO;
import com.daxia.ogmall.dto.SpecificationDTO;
import com.daxia.ogmall.service.ProductPropertiesService;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/productProperties", produces="text/html;charset=UTF-8")
public class AdminProductPropertiesController extends BaseController {

	@Autowired
	private ProductPropertiesService productPropertiesService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id, Long sId) {
		SpecificationDTO s = specificationService.load(sId);
		map.put("s", s);
		if (id != null) {
			ProductPropertiesDTO dto = productPropertiesService.load(id);
			map.put("n", dto);
		}
		return "admin/productProperties/productProperties_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(ProductPropertiesDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getId() == null) {
			productPropertiesService.create(dto);
		} else {
			productPropertiesService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		productPropertiesService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	public String list(ProductPropertiesDTO dto, Map<String, Object> map, Page page) {
		List<ProductPropertiesDTO> dtos = productPropertiesService.find(dto, page);
		Logs.COMMON.debug(dtos.size());
		map.put("list", dtos);
		map.put("query", dto);
		map.put("sId", dto.getSpecification().getId());
		return "admin/productProperties/productProperties_list";
	}
	
}
